﻿using ApplicationModels.A07.Chats;
using AutoMapper;
using Commons.Responses;
using EntityModels.E07.Chats;
using IApplicationServices;
using IRepositories;
using System.Linq.Expressions;

namespace ApplicationService
{
    public class MessageService:BaseApplicationService<int,Message,MessageAM>,IMessageService
    {
        public MessageService(IBaseRepository<int, Message> repository, IMapper mapper) : base(repository, mapper)
        {
        }

        public override async Task<DatasResponse<MessageAM>> GetAllAsync(Expression<Func<Message, bool>> predicate)
        {
            var res =  await Repository.GetAllAsync(predicate, new Dictionary<string, bool> { { "CreationTime", false } });
            var apibos = Mapper.Map<List<MessageAM>>(res.Datas);
            return new DatasResponse<MessageAM>(apibos);
        }

        public async Task<MessageResponse> SendMessageAsync(ConversationAM conversation, MessageAM message)
        {
            conversation.LastUpdated = DateTime.Now;
            conversation.LastMessage = message.Content;
            await UpdateOtherAsync<int, Conversation, ConversationAM>(conversation);
            return await AddAsync(message);
        }
    }
}
